package converters;

import java.util.Iterator;

import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.convert.Converter;
import javax.faces.convert.FacesConverter;

import modelo.Caracteristica;
import services.CaractersticaService;

@FacesConverter("caracteristicaConverter")
public class CaracteristicaConverter implements Converter {

	public Object getAsObject(FacesContext fc, UIComponent uic, String value) {
        if(value != null && value.trim().length() > 0) {
            CaractersticaService service = (CaractersticaService) fc.getExternalContext().getApplicationMap().get("caracteristicaService");
            Iterator<Caracteristica> i=service.getListCaracteristica().iterator();
            while (i.hasNext()) {
				Caracteristica c = (Caracteristica) i.next();
				if(c.getIdCaracteristica() == Integer.parseInt(value))
					return c;
			}
            return null;
        }
        else {
            return null;
        }
    }
 
    public String getAsString(FacesContext fc, UIComponent uic, Object object) {
        if(object != null) {
        	return String.valueOf(((Caracteristica) object).getIdCaracteristica());
        }
        else {
            return null;
        }
    }  
}
